package com.hospital.pony.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hospital.R;
import com.hospital.ReviewShareMessageActivity;
import com.hospital.pony.model.TopicModel;
import com.hospital.pony.view.GridviewForScrollView;

import java.util.List;


public class TopicAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<TopicModel> list_result;
    private int posIndex;
    private Context mContext;

    public TopicAdapter(Context context, List<TopicModel> list_result ) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        this.list_result = list_result;
    }

    @Override
    public int getCount() {
        return list_result.size();
    }

    @Override
    public Object getItem(int position) {
        return list_result.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_topic, null);
            holder = new ViewHolder();
            holder.guide_message = (TextView) convertView.findViewById(R.id.guide_message);
            holder.guide_title = (TextView) convertView.findViewById(R.id.guide_title);
            holder.lostTime = (TextView) convertView.findViewById(R.id.lostTime);

            holder.musicImage = (ImageView) convertView.findViewById(R.id.musicImage);
            holder.mgvTopicImg = (GridviewForScrollView) convertView.findViewById(R.id.mgvTopicImg);
            holder.userInfo = (LinearLayout) convertView.findViewById(R.id.userInfo);



            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }



        String inforTopic =list_result.get(position).getTopicInfor();
        holder.guide_message.setText(inforTopic);
        holder.guide_title.setText(list_result.get(position).getTopicUserName());

        holder.lostTime.setText(list_result.get(position).getTopicTime());

        if (list_result.get(position).getTopicImg().length() > 2) {
            holder.mgvTopicImg.setVisibility(View.VISIBLE);
            TopicImgAdapter topicImgAdapter = new TopicImgAdapter(mContext, list_result.get(position).getTopicImg().split(","));
            holder.mgvTopicImg.setAdapter(topicImgAdapter);
        } else {
            holder.mgvTopicImg.setVisibility(View.GONE);
        }


        holder.userInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, ReviewShareMessageActivity.class);
                intent.putExtra("msg", list_result.get(position));
                mContext.startActivity(intent);
            }
        });

//        if (!TextUtils.isEmpty(list_result.get(position).getTopicImg())) {
//            Picasso.with(mContext).load(Consts.URL_IMAGE + list_result.get(position).getTopicImg()).placeholder(R.drawable.default_drawable_show_pictrue).into(holder.musicImage);
//        }


        return convertView;

    }

    private class ViewHolder {
        private ImageView impraise, imcollect, imreview;
        private TextView guide_message;
        private TextView guide_title;
        private TextView type_message;
        private TextView lostTime;
        private LinearLayout userInfo;
        private ImageView musicImage;
        private GridviewForScrollView mgvTopicImg;

    }

    public void setPos(int pos) {
        posIndex = pos;
    }

}
